﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.TR.Bussiness;
using ZX.TR.Library;
using ZX.EHR.Library;
using DevExpress.Charts;
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Web;
using System.Data;
using ZX.Web.UI.Controls;
using System.Text;

namespace ZX.TR.Web
{
    public partial class ExportToExcel : BasePage
    {
        string questionnaireID = "";
        BTrQuestionnaireSubject bTrQuestionnaireSubject = new BTrQuestionnaireSubject();
        V_TrQuestionnaire v_TrQuestionnaire = new V_TrQuestionnaire();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                questionnaireID = Session["trquestion"].ToString();
                if (questionnaireID != "")
                {
                    v_TrQuestionnaire = bTrQuestionnaireSubject.GetAllQuestionnaireInfoById(new Guid(questionnaireID));
                    if (v_TrQuestionnaire != null)
                    {
                        lblQname.Text = v_TrQuestionnaire.QuestionnaireName;
                        lblQtype.Text = v_TrQuestionnaire.DeptName;
                        lbltotalPeopleNum.Text = bTrQuestionnaireSubject.GetAnswerQuestionnaireTotalPeopleNum(questionnaireID).ToString();
                        lblEffectivetotalPeopleNum.Text = bTrQuestionnaireSubject.GetEffectiveTotalPeopleNum(questionnaireID).ToString();
                    }
                    bind(Session["trquestion"] == null ? Guid.NewGuid() : new Guid(Session["trquestion"].ToString()));
                }
            }
            Response.Clear();
            Response.Buffer = true;
            string fileName = v_TrQuestionnaire.QuestionnaireName+"_结果";

            Response.AppendHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/vnd.ms-excel";
            this.EnableViewState = false; 
        }

        private void bind(Guid guid)
        {
            DatUser duser = Session["User"] as DatUser;
            string condcondition = " SubjectType in ('单选题','多选题')";
            condcondition = " 1=1";
            IList<V_TrExam> lstExam = bTrQuestionnaireSubject.GetExamListById(guid, condcondition);
            rptList.DataSource = lstExam;
            rptList.DataBind();
        }

        #region 题目初始化
        /// <summary>
        /// 控件绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lblType = e.Item.FindControl("lblType") as Label;
            HiddenField subjectInfoID = e.Item.FindControl("TrSubjectInfoID") as HiddenField;
            WebChartControl charSubjectInfo = e.Item.FindControl("charSubjectInfo") as WebChartControl;
            HyperLink lnkOpen = e.Item.FindControl("lnkOpen") as HyperLink;

            if (lblType != null && lblType.Text == "填空题")
            {
                Repeater rptEssayQuestionItem = e.Item.FindControl("rptEssayQuestionItem") as Repeater;

                rptEssayQuestionItem.DataSource = bTrQuestionnaireSubject.GetEssayQuestion(questionnaireID, subjectInfoID.Value);
                rptEssayQuestionItem.DataBind();

            }

            if (lblType != null && (lblType.Text == "单选题" || lblType.Text == "多选题"))
            {
                Repeater rpt = e.Item.FindControl("rptItem") as Repeater;
                if (subjectInfoID != null)
                {
                    //取得数据源
                    DataTable dt = bTrQuestionnaireSubject.GetAnswerQuestionnaire(questionnaireID, subjectInfoID.Value);
                    //生成列表
                    rpt.DataSource = dt;
                    rpt.DataBind();
                }
            }
        }

        #endregion

        #region 计算比例
        /// <summary>
        /// 比例
        /// </summary>
        /// <returns></returns>
        public string GetCountPercent(string subjectInfoID, string answer)
        {
            decimal totalPeopleNum = 0, totalItemPeopleNum = 0, percent = 0;
            decimal.TryParse(lblEffectivetotalPeopleNum.Text, out totalPeopleNum);
            totalItemPeopleNum = bTrQuestionnaireSubject.GetAnswerJudgeTotalPeopleNum(questionnaireID, subjectInfoID, answer);
            if (totalPeopleNum > 0)
            {
                percent = Math.Round(totalItemPeopleNum / totalPeopleNum * 100, 2);
            }
            return percent.ToString() + "%";
        }

        /// <summary>
        /// 人数
        /// </summary>
        /// <returns></returns>
        public string GetCount(string subjectInfoID, string answer)
        {
            return bTrQuestionnaireSubject.GetAnswerJudgeTotalPeopleNum(questionnaireID, subjectInfoID, answer).ToString();
        }

        #endregion
    }
}